#!/bin/sh

# errors shouldn't cause script to exit
set +e

# add rserver user account
useradd -r -U rstudio-server

# create softlink to admin script in /usr/sbin
RSERVER_ADMIN_SCRIPT=/usr/sbin/rstudio-server
ln -f -s "${CMAKE_INSTALL_PREFIX}"/bin/rstudio-server $RSERVER_ADMIN_SCRIPT

# create config directory and default config files
mkdir -p /etc/rstudio
if ! test -f /etc/rstudio/rserver.conf
then
   printf '# Server Configuration File\n\n' > /etc/rstudio/rserver.conf
fi
if ! test -f /etc/rstudio/rsession.conf
then
   printf '# R Session Configuration File\n\n' > /etc/rstudio/rsession.conf
fi
if ! test -f /etc/rstudio/database.conf
then
  cp ${CMAKE_INSTALL_PREFIX}/extras/conf/database.conf /etc/rstudio/
  chown rstudio-server /etc/rstudio/database.conf
  chmod 600 /etc/rstudio/database.conf
fi

mkdir -p /etc/rstudio/themes
if ! test -f /etc/rstudio/themes/README.md
then
   cp ${CMAKE_INSTALL_PREFIX}/extras/themes/README.md /etc/rstudio/themes/README.md
fi
mkdir -p /etc/rstudio/fonts
if ! test -f /etc/rstudio/fonts/README.md
then
   cp ${CMAKE_INSTALL_PREFIX}/extras/fonts/README.md /etc/rstudio/fonts/README.md
fi

# create var directories
mkdir -p /var/run/rstudio-server
mkdir -p /var/lock/rstudio-server
mkdir -p /var/log/rstudio/rstudio-server
mkdir -p /var/lib/rstudio-server
mkdir -p /var/lib/rstudio-server/conf
mkdir -p /var/lib/rstudio-server/body
mkdir -p /var/lib/rstudio-server/proxy

# suspend all sessions
rstudio-server force-suspend-all > /dev/null 2>&1

# if /etc/os-release exists, source it to learn a bit more about what platform
# we're on
if test -f /etc/os-release
then
   . /etc/os-release
fi

# create openssl softlinks based off SuSE version
if test -f /etc/SuSE-release || test "$ID_LIKE" = "suse"
then

   # if we weren't provided a version by /etc/os-release, then
   # try to find it in /etc/SuSE-release
   if test -z "$VERSION_ID"; then
      VERSION_ID="$(grep "VERSION" /etc/SuSE-release | sed 's/[^0-9,.]*//g')"
   fi

   # discover the version of OpenSSL that we expect to find on this system
   compare_versions () { test "$(echo "$@" | tr " " "\\n" | sort -V | head -n 1)" != "$1"; }
   if compare_versions "$VERSION_ID" "11"
   then
      sslversion=1.0.0
   else
      sslversion=0.9.8
   fi

   # look for these libraries in the common places
   for dir in /usr/lib64 /lib64 /usr/lib /lib
   do
      if test -f $dir/libssl.so.$sslversion -a -f $dir/libcrypto.so.$sslversion
      then
         for file in libssl.so libcrypto.so
         do
            ln -s -f $dir/$file.$sslversion "${CMAKE_INSTALL_PREFIX}"/bin/$file.10
         done
         break
      fi
   done

fi

# determine which init system is in use
# a variation on:
# https://wiki.ubuntu.com/SystemdForUpstartUsers#How_to_identify_which_init_system_you_are_currently_booting_with
INIT_SYSTEM="unknown"
if ps -p1 | grep systemd > /dev/null 2>&1; then
    # If pid 1 is systemd, this is systemd.
    INIT_SYSTEM="systemd"
elif ps -p1 | grep init > /dev/null 2>&1; then
    # If pid 1 is init and init tells us it is upstart, this is upstart.
    if /sbin/init --version | grep upstart > /dev/null 2>&1; then
        INIT_SYSTEM="upstart"
    fi
fi

# add systemd, upstart, or init.d script and start the server
if test "$INIT_SYSTEM" = "systemd"
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server
   
   systemctl stop rstudio-server.service 2>/dev/null
   systemctl disable rstudio-server.service 2>/dev/null

   if test -e /lib/systemd
   then
      SYSTEMD_PREFIX="/lib/systemd"
   else
      SYSTEMD_PREFIX="/usr/lib/systemd"
   fi

   cp ${CMAKE_INSTALL_PREFIX}/extras/systemd/rstudio-server.redhat.service $${EMPTY}{SYSTEMD_PREFIX}/system/rstudio-server.service

   systemctl daemon-reload
   systemctl enable rstudio-server.service
   $RSERVER_ADMIN_SCRIPT start

   sleep 1
   systemctl --no-pager status rstudio-server.service
elif test "$INIT_SYSTEM" = "upstart"
then
   # remove any previously existing init.d based scheme
   service rstudio-server stop 2>/dev/null
   rm -f /etc/init.d/rstudio-server

   cp ${CMAKE_INSTALL_PREFIX}/extras/upstart/rstudio-server.redhat.conf /etc/init/rstudio-server.conf
   initctl reload-configuration
   initctl stop rstudio-server 2>/dev/null
   $RSERVER_ADMIN_SCRIPT start
else
   if test -f /etc/SuSE-release
   then
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/suse/rstudio-server /etc/init.d/
   else
      cp ${CMAKE_INSTALL_PREFIX}/extras/init.d/redhat/rstudio-server /etc/init.d/
   fi
   chmod +x /etc/init.d/rstudio-server
   chkconfig --add rstudio-server
   service rstudio-server stop 2>/dev/null
   $RSERVER_ADMIN_SCRIPT start
fi

# add pam profile
if [ ! -e /etc/pam.d/rstudio ]
then
   cp ${CMAKE_INSTALL_PREFIX}/extras/pam/rstudio /etc/pam.d/
fi

# clear error termination state
set -e
